﻿
namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.HighLevel
{
    [ModuleDisplay("Volyme")]
    public class MonoAmpModule : BaseModule
    {
        public readonly ModuleInput Input;
        public readonly ModuleInput InVolym;

        public readonly ModuleOutput Output;

        public readonly InputModuleParameter ParameterVolyme;

        public MonoAmpModule(ModuleManager manager)
            : base(manager)
        {
            AddInput(Input = new ModuleInput("Input", IOType.AudioOrWaveform, this));
            AddInput(InVolym = new ModuleInput("Volyme controller", IOType.AudioOrWaveform, this));
            AddOutput(Output = new ModuleOutput("Output", IOType.AudioOrWaveform, this));

            ParameterVolyme = new InputModuleParameter(this, "Vol", 0.2, IOType.AudioOrWaveform);
            ParameterVolyme.Label = "Volyme";
            ParameterVolyme.MinValue = 0;
            ParameterVolyme.MaxValue = 1;
            AddModuleParameter(ParameterVolyme);
        }

        public override void Process()
        {
            if (Input.IsConnected)
            {
                // If VolymeController is connected.
                if (InVolym.IsConnected)
                {
                    for (int i = Input.FromOutput.AudioBuffer.Length - 1; i >= 0; i--)
                    {
                        Output.AudioBuffer[i] = Input.FromOutput.AudioBuffer[i] * ParameterVolyme[i] * InVolym.FromOutput.AudioBuffer[i];
                    }
                }
                else
                {
                    for (int i = Input.FromOutput.AudioBuffer.Length - 1; i >= 0; i--)
                    {
                        Output.AudioBuffer[i] = Input.FromOutput.AudioBuffer[i] * ParameterVolyme[i];
                    }
                }
            }
            else
            {
                System.Array.Clear(Output.AudioBuffer.Buffer, 0, Output.AudioBuffer.Buffer.Length);
            }
        }
    }
}
